package cc.chinagps.gateway.unit.eg.upload.bean;

import lombok.Data;

@Data
public class JMCDTCInfo {
    int dtcCode;
    int failureType;
    int statusCover;

    public JMCDTCInfo() {
    }

    public JMCDTCInfo(int dtcCode, int failureType, int statusCover) {
        this.dtcCode = dtcCode;
        this.failureType = failureType;
        this.statusCover = statusCover;
    }

    public String getFaultCode() {
        byte[] faultCode = new byte[3];
        faultCode[0] = (byte) ((dtcCode >> 8) & 0xFF);
        faultCode[1] = (byte) (dtcCode & 0xFF);
        faultCode[2] = (byte) failureType;

        String codeStr = toHexStr(faultCode);
        String code0Str = "";
        int code0 = (dtcCode & 0xFFFF) >> 14;
        switch (code0) {
            case 0:
                code0Str = "P";
                break;
            case 1:
                code0Str = "C";
                break;
            case 2:
                code0Str = "B";
                break;
            case 3:
                code0Str = "U";
                break;
            default:
                break;
        }

        String code1Str;
        int code1 = ((dtcCode & 0xFFFF) >> 12) & 0x03;
        code1Str = code1 + "";

        return code0Str + code1Str + codeStr.substring(1);
    }

    private String toHexStr(byte[] data) {
        StringBuilder sb = new StringBuilder(data.length * 2);
        for (int i = 0; i < data.length; i++) {
            if ((data[i] & 0xff) < 0x10) {
                sb.append('0');
            }
            sb.append(Integer.toHexString(data[i] & 0xff).toUpperCase());
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        JMCDTCInfo jmcdtcInfo = new JMCDTCInfo((short) 0x9000, 0x16, 0);
        System.out.println(jmcdtcInfo.getFaultCode());
        Double.doubleToRawLongBits(0.0D);
    }
}